package manager;

import host.impl.AdminHost;
import host.impl.Host;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import properties.PropertyManager;

import stop.StopManager;

import RMI.adminTOrtdg.impl.Receiver;
import RMI.rtdgTOadmin.impl.Sender;
import RTDGDiscovery.DiscoveryDispatcher;
import RTDGDiscovery.DiscoveryListener;
import enums.RTDGRequestType;

public class ServiceManager{
	public static ArrayList<AdminHost> adminHosts;
	public static DiscoveryListener discoveryListener;
	public static DiscoveryDispatcher discoveryDispatcher;
	public static Receiver rmiReceiver;
	public static Sender rmiSender;
	private boolean automaticDiscover;
	public static Host thisHost;
	public static String PID;
	public static String name;
	
	static Logger log = Logger.getLogger("rien");
	 
	public static void printLog(int priority, String txt){
		System.out.println(txt);
		
		switch (priority) {
		case Priority.INFO_INT:
				log.info(txt);
			break;
		case Priority.ERROR_INT:
				log.error(txt);
		break;
		case Priority.WARN_INT:
				log.warn(txt);
			break;
		case Priority.FATAL_INT:
				log.fatal(txt);
			break;
		default:
			break;
		}
	}
	
	public void setName(String name) {
		ServiceManager.name = name;
	}

	public void setPID(String pID) {
		PID = pID;
	}

	public void setThisHost(Host thisHost) {
		ServiceManager.thisHost = thisHost;
	}

	public void setAdminHosts(ArrayList<AdminHost> adminHosts) {
		ServiceManager.adminHosts = adminHosts;
	}

	public void setDiscoveryListener(DiscoveryListener discoveryListener) {
		ServiceManager.discoveryListener = discoveryListener;
	}


	public void setDiscoveryDispatcher(DiscoveryDispatcher discoveryDispatcher) {
		ServiceManager.discoveryDispatcher = discoveryDispatcher;
	}

	public void setRmiReceiver(Receiver rmiReceiver) {
		ServiceManager.rmiReceiver = rmiReceiver;
	}


	public void setRmiSender(Sender rmiSender) {
		ServiceManager.rmiSender = rmiSender;
	}


	public boolean isAutomaticDiscover() {
		return automaticDiscover;
	}

	public void setAutomaticDiscover(boolean automaticDiscover) {
		this.automaticDiscover = automaticDiscover;
	}
	
	public void initServices(){
		
		adminHosts = new ArrayList<AdminHost>();
		System.out.println("PROCESSUS pere: "+ManagementFactory.getRuntimeMXBean().getName());
		PID = ManagementFactory.getRuntimeMXBean().getName().substring(0,ManagementFactory.getRuntimeMXBean().getName().indexOf('@'));
		if(automaticDiscover){
			try {
				discoveryDispatcher.discover();
			} catch (IOException e) {
				ServiceManager.printLog(Priority.ERROR_INT,PropertyManager.properties.getProperty("discoveryError"));
				e.printStackTrace();
			}
		}
		try {
			ServiceManager.thisHost.setIpAddress(InetAddress.getLocalHost().getHostAddress());
			ServiceManager.printLog(Priority.INFO_INT,"IP LOCAL: "+ServiceManager.thisHost.getIpAddress());
		} catch (UnknownHostException e) {
			ServiceManager.printLog(Priority.FATAL_INT,PropertyManager.properties.getProperty("localIpError"));
			e.printStackTrace();
			System.exit(0);
		}
	}
	
	public ServiceManager() {
	}

	public static void stopService() {
		for (int i = 0; i < adminHosts.size(); i++) {
			try {
				ServiceManager.printLog(Priority.INFO_INT, PropertyManager.properties.getProperty("adminHostNumber")+" "+adminHosts.size());
				ServiceManager.rmiSender.send(RTDGRequestType.UNREGISTER, adminHosts.get(i).getIpAddress(), adminHosts.get(i).getPortNumber());
			} catch (RemoteException e) {
				ServiceManager.printLog(Priority.ERROR_INT, PropertyManager.properties.getProperty("errorOnAdminNotification"));
				e.printStackTrace();
				System.exit(0);
			}
		}

		StopManager.stopMe();
	}
}
